package setting

import (
	"fmt"
	"gopkg.in/yaml.v2"
	"os"
)

type Controller struct {

}

func (c *Controller) Init (path string) (Setting, error) {

	var setting Setting

	// 从配置文件中读取出[]byte
	// pwd, _ := os.Getwd()
	// fmt.Println("pwd: ", pwd)
	// ReadFile的工作目录在项目根目录即 ginx 下，而非main.go所在目录。
	// 因此ReadFile()传入的路径不需要相对main.go取../conf/，而只需要基于项目根路径 ginx 取 conf/ 即可
	readFile, errReadFile := os.ReadFile(path)
	if errReadFile != nil {
		fmt.Println("Read Fail", errReadFile)
		return setting, errReadFile
	}
	//fmt.Println("ff:", readFile)

	// []byte ==yaml.Unmarshal==> struct
	errUnmarshal := yaml.Unmarshal(readFile, &setting)
	if errUnmarshal != nil {
		fmt.Println("errUnmarshal:", errUnmarshal)
		// 日志由应用负责打，底层不打日志，底层只将错误上送到应用
		return setting, errUnmarshal
	}
	return setting, nil

}

